<template>
  <div class="com-container">
    <div class="com-chart" ref="hotRef"></div>
    <span class="iconfont arrLeft" @click="toLeft" :style="comStyle"
      >&#xe6ef;</span
    >
    <span class="iconfont arrRight" @click="toRight" :style="comStyle"
      >&#xe6ed;</span
    >
    <span class="cate-name" :style="comStyle">{{ catName }}</span>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getThemeValue } from "@/utils/theme_utils";

export default {
  data() {
    return {
      allData: null,
      myEchart: null,
      //当前展示分类的索引
      currentIndex: 0,
      titleFontSize: 0,
    };
  },
  created() {
    this.$socket.registerCallBack("hotData", this.getData);
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: "getData",
      socketType: "hotData",
      chartName: "hotproduct",
      value: "",
    });
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("hotData");
  },
  computed: {
    ...mapState(["theme"]),
    /***************计算当前显示的分类名称*****************/
    catName() {
      return this.allData ? this.allData[this.currentIndex].name : "";
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize * 2 + "px",
        color: getThemeValue(this.theme).titleColor,
      };
    },
  },

  watch: {
    theme() {
      console.log("主题切换了");
      //销毁当前图表
      this.myEchart.dispose();
      //以最新的主题初始化图表对象
      this.initChart();
      //屏幕适配
      this.screenAdapter();
      //渲染数据
      this.updateChart();
    },
  },
  methods: {
    /******************初始化***********************/
    initChart() {
      this.myEchart = this.$echarts.init(this.$refs.hotRef, this.theme);
      const initOption = {
        title: {
          text: "▎热销商品占比",
          top: "3%",
          left: "3%",
          textStyle: {
            fontSize: 60,
          },
        },
        legend: {
          top: "15%",
          icon: "circle",
          left: "center",
        },
        tooltip: {},
        series: [
          {
            type: "pie",
            label: false,
            //饼图处于高亮情况下 应用的一些样式
            emphasis: {
              label: {
                show: true,
              },
              labelLine: {
                show: false,
              },
            },
          },
        ],
      };
      this.myEchart.setOption(initOption);
    },
    /********************获取数据*********************/
    getData(res) {
      // const { data: res } = await this.$http.get("hotproduct");
      this.allData = res;
      this.updateChart();
    },
    /*******************更新图表**********************/
    updateChart() {
      const legendData = this.allData[this.currentIndex].children.map(
        (item) => item.name
      );
      const seriesData = this.allData[this.currentIndex].children.map(
        (item) => {
          return {
            name: item.name,
            value: item.value,
          };
        }
      );
      // console.log(this.titleFontSize);
      const dataOption = {
        legend: {
          data: legendData,
        },
        title: {
          textStyle: {
            fontSize: this.titleFontSize * 1.5,
          },
        },
        series: [
          {
            data: seriesData,
          },
        ],
      };
      this.myEchart.setOption(dataOption);
    },
    /********************屏幕适配*********************/
    screenAdapter() {
      this.titleFontSize = (this.$refs.hotRef.offsetWidth / 100) * 3.4;
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          //图例之间的间隔
          itemGap: this.titleFontSize / 2,
          textStyle: {
            fontSize: this.titleFontSize / 1.1,
          },
        },
        series: {
          radius: this.titleFontSize * 4.5,
          center: ["50%", "60%"],
        },
      };
      this.myEchart.setOption(adaptOption);
      this.myEchart.resize();
    },
    /********************右箭头的事件*********************/
    toLeft() {
      this.currentIndex--;
      if (this.currentIndex < 0) this.currentIndex = this.allData.length - 1;
      console.log(this.currentIndex);
      this.updateChart();
    },
    /*******************右箭头的事件**********************/
    toRight() {
      this.currentIndex++;
      if (this.currentIndex >= this.allData.length) this.currentIndex = 0;
      console.log(this.currentIndex);
      this.updateChart();
    },
  },
};
</script>

<style lang="less" scoped>
.com-container {
  position: relative;
}
.arrLeft {
  position: absolute;
  left: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  color: #fff;
  z-index: 999;
}
.arrRight {
  position: absolute;
  right: 10%;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  z-index: 999;
  color: #fff;
}
.cate-name {
  position: absolute;
  left: 80%;
  bottom: 20px;
  color: #fff;
}
</style>